package com.syqc.monitor.ui.carinfo

import android.view.Gravity
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.adapter.LocationCarAdapter
import com.syqc.adapter.OwnerAdapter
import com.syqc.adapter.TmnInfoAdapter
import com.syqc.comlib.GuideControl.isBaseCar
import com.syqc.comlib.base.LazyFragment
import com.syqc.comlib.rx.filterFast
import com.syqc.custom.CarInfoTextView
import com.syqc.entity.OnlyCarInfo
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentCarBaseBinding
import com.syqc.monitor.popup.DriversPopup
import com.syqc.utils.TimeUtil
import com.syqc.utils.ToolsUtil


/** @Author Jasper @Time 2021/2/27 @Desc 定位基础信息 */
class CarBaseFragment : LazyFragment<FragmentCarBaseBinding>(FragmentCarBaseBinding::inflate) {
    private val infoAdapter by lazy { CarInfoAdapter() }
    private val locationAdapter by lazy { LocationCarAdapter() }
    private val mViewModel by viewModels<OnlyCarVM>()
    private val mAvtVM by activityViewModels<CarInfoViewModel>()
    private val mDriversPopup by lazy { DriversPopup(requireContext()) }

    private val tmnAdapter by lazy { TmnInfoAdapter() }

    private val ownerAdapter by lazy {
        OwnerAdapter(driverPhone = {
            ToolsUtil.talkPhone(requireContext(), mViewModel.firstPhone)
        }, showDrivers = {
            mDriversPopup.show(requireActivity().window.decorView, mViewModel.driverInfo)
        })
    }
    private var mCarPopup: CarBasePopup? = null


    override fun initView(view: View) {
        mViewModel.carId = mAvtVM.carId
        mViewModel.getGprR()
        mViewModel.getCarInfo()

        binding.rvCarInfo.layoutManager = LinearLayoutManager(requireContext())
        binding.rvCarInfo.adapter = infoAdapter

        binding.rvLocation.layoutManager = LinearLayoutManager(requireContext())
        binding.rvLocation.adapter = locationAdapter

        binding.rvOwnerDriver.layoutManager = LinearLayoutManager(requireContext())
        binding.rvOwnerDriver.adapter = ownerAdapter

        binding.rvTmnInfo.layoutManager = LinearLayoutManager(requireContext())
        binding.rvTmnInfo.adapter = tmnAdapter
        if (isBaseCar) {
            mCarPopup = CarBasePopup(requireContext())
            mCarPopup?.showAtLocation(binding.root, Gravity.NO_GRAVITY, 0, 0)
            isBaseCar = false
        }
    }

    override fun initData() {
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)

        mViewModel.carLive.observe(this) {
            infoAdapter.setList(it)
            binding.nsvInfo.isVisible = true

            binding.rdbLocation.isVisible = mViewModel.mainTmn != null
            binding.rdbVideo.isVisible = mViewModel.videoTmn != null
            binding.rdbAdas.isVisible = mViewModel.adasTmn != null
            val isTmnShow =
                !(mViewModel.mainTmn == null && mViewModel.videoTmn == null && mViewModel.adasTmn == null)

            binding.rdgTmn.isVisible = isTmnShow
            binding.rvTmnInfo.isVisible = isTmnShow
            binding.tvOwner.isVisible = isTmnShow
            if (mViewModel.mainTmn != null) {
                tmnAdapter.setList(mViewModel.mainTmn?.keyValues)
                binding.rdbLocation.isChecked=true
            } else if (mViewModel.videoTmn != null) {
                tmnAdapter.setList(mViewModel.videoTmn?.keyValues)
                binding.rdbVideo.isChecked=true
            } else if (mViewModel.adasTmn != null) {
                tmnAdapter.setList(mViewModel.adasTmn?.keyValues)
                binding.rdbAdas.isChecked=true
            }

            ownerAdapter.driverInfo = mViewModel.driverInfo
        }

        mViewModel.locationLive.observe(this) {
            locationAdapter.setList(it)
        }

        mViewModel.ownerLive.observe(this) {
            ownerAdapter.setList(it)
        }
    }

    override fun initEvent() {

        binding.rdbLocation.filterFast {
            tmnAdapter.setList(mViewModel.mainTmn?.keyValues)
        }
        binding.rdbVideo.filterFast {
            tmnAdapter.setList(mViewModel.videoTmn?.keyValues)
        }

        binding.rdbAdas.filterFast {
            tmnAdapter.setList(mViewModel.adasTmn?.keyValues)
        }
    }
    override fun onDestroyView() {
        super.onDestroyView()
        if (mCarPopup != null && mCarPopup?.isShowing == true) {
            mCarPopup?.dismiss()
            mCarPopup = null
        }
    }
}

class CarInfoAdapter :
    BaseQuickAdapter<OnlyCarInfo, BaseViewHolder>(R.layout.item_car_info) {
    override fun convert(holder: BaseViewHolder, item: OnlyCarInfo) {
        val citvInfo = holder.getView<CarInfoTextView>(R.id.citvInfo)
        citvInfo.setRightText(item.value)
        citvInfo.setLeftTitle(item.type)
        if (holder.layoutPosition == 6) {
            if (item.value.isNotEmpty()) {
                val isOver = TimeUtil.instance.isSimpleOverNow(item.value)
                citvInfo.setRightTextColor(if (isOver) R.color.color_00F else R.color.color_RED)
            }
            citvInfo.setRightWeight(2f)
        } else {
            citvInfo.setRightTextColor(R.color.color_666)
            citvInfo.setRightWeight(3f)
        }
    }
}